package com.lanren.familybookapp.ui.me.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.common.utils.GsonUtils;
import com.lanren.common.utils.LogUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.easyhttp.body.UIProgressResponseCallBack;
import com.lanren.easyhttp.callback.ProgressDialogCallBack;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.easyhttp.subsciber.IProgressDialog;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.app.UserInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IOneselfModel;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName OneselfModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/11/3 15:32
 * @Version 1.0
 */
public class OneselfModel extends BaseModel {
    public OneselfModel() {
        initDispoable();
    }

    public void updateAvatar(String avatarPath, IProgressDialog mProgressDialog, UIProgressResponseCallBack listener) {
        File file = new File(avatarPath);
        Disposable disposable = HttpManager.post(ApiUrl.API.USER_INFO_UPDATE)
                .headers(AppUserHelper.getHttpHeaders())
                .params("userAvatar", file, file.getName(), listener)
                .execute(new ProgressDialogCallBack<UserInfoBean>(mProgressDialog, true, true) {
                    @Override
                    public void onError(ApiException e) {
//                        super.onError(e);
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        AppUserHelper.getInstance().setUserInfoBean(userInfoBean);
                    }
                });
        addDisposable(disposable);
    }


    public void updateUserInfo(String name, String sex, String age, final IOneselfModel.OneselfModel listener) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            map.put("userName", name);
        }
        if (StringUtils.isNotBlank(sex)) {
            map.put("userSex", sex);
        }
        if (StringUtils.isNotBlank(age)) {
            map.put("userAge", age);
        }
        Disposable disposable = HttpManager.post(ApiUrl.API.USER_INFO_UPDATE)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        listener.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        AppUserHelper.getInstance().setUserInfoBean(userInfoBean);
                        LogUtils.d("login=" + GsonUtils.toJson(userInfoBean));
                        listener.onSuccess();
                    }
                });
        addDisposable(disposable);

    }
}
